package com.sms.client.ui;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.sms.client.ServiceUtil;
import com.sms.client.utils.LoadingBox;
import com.sms.shared.SessionInfo;

public class LoginUI {
	private PopupPanel popup;
	private TextBox userName;
	private PasswordTextBox password;
	private Button loginButton;
	private FlexTable mainTable;
	private AsyncCallback asyncCallBack;
	
	public LoginUI(int left,int top,AsyncCallback<SessionInfo> asyncCallback){
		this.asyncCallBack = asyncCallback;
		
		mainTable = new FlexTable();
		userName = new TextBox();
		password = new PasswordTextBox();
		
		password.addKeyPressHandler(new KeyPressHandler() {
			
			@Override
			public void onKeyPress(KeyPressEvent event) {
				int code = ((int) event.getCharCode());
				if (code == 13 || code == 40) { // User hit "Enter" or "Down arrow" key
					callLogin();
				}
				
			}
		});

		loginButton = new Button("Go");
		loginButton.setStyleName("btn-blue");
		userName.setFocus(true);
		mainTable.setWidget(0, 0, new HTML("<b>User Name</b>"));
		mainTable.setWidget(0, 1, new HTML("<b>Password</b>"));
		mainTable.setWidget(0, 2, new HTML());
		mainTable.setWidget(1, 0, userName);
		mainTable.setWidget(1, 1, password);
		mainTable.setWidget(1, 2, loginButton);
		mainTable.setWidget(2, 0, new HTML());
		mainTable.getFlexCellFormatter().setColSpan(2, 0, 3);
		popup = new PopupPanel();
		popup.add(mainTable);
		popup.setAutoHideEnabled(true);
		popup.setPopupPosition(left, top);
		popup.show();
		
		loginButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				callLogin();
			}
		});

	}
	
	private void callLogin(){
		
		boolean isValid = true;
		if(userName.getText() == null || userName.getText().trim().length() == 0){
			HTML html = (HTML)mainTable.getWidget(2, 0);
			html.setHTML("<font color=\"red\">Enter user name</font>");
			isValid =false;
		}
		
		if(isValid && (password.getText() == null || password.getText().trim().length() == 0)){
			HTML html = (HTML)mainTable.getWidget(2, 0);
			html.setHTML("<font color=\"red\">Enter password</font>");
			isValid =false;
		}
		if(isValid){
			LoadingBox.getSharedInstance().show();
			HTML html = (HTML)mainTable.getWidget(2, 0);
			html.setHTML("");
			
			ServiceUtil.getUserLoginServiceAsync().checkLogin(userName.getText(), password.getText(), new AsyncCallback<List>() {

				@Override
				public void onFailure(Throwable caught) {
					LoadingBox.getSharedInstance().hide();
					asyncCallBack.onFailure(caught);
					
				}

				@Override
				public void onSuccess(List result) {
					LoadingBox.getSharedInstance().hide();
					Object object = result.get(0);
					if(object instanceof String){
						HTML html = (HTML)mainTable.getWidget(2, 0);
						html.setHTML("<font color=\"red\">User Name/Password is incorrect!</font>");
					}else{
						popup.hide();
						asyncCallBack.onSuccess((SessionInfo)result.get(0));
						
					}
					
				}
			});
		}
	}
	
}
